package com.www.applyword.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewStub;

import androidx.annotation.Nullable;
import androidx.recyclerview.widget.DefaultItemAnimator;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.www.applyword.Constants.Constants;
import com.www.applyword.R;
import com.www.applyword.adapter.HotJobAdapter;
import com.www.applyword.base.BaseFragment;
import com.www.applyword.bean.HotJobBean;
import com.www.applyword.bean.OrderBean;
import com.www.applyword.event.EventConstants;
import com.www.applyword.event.MessageEvent;
import com.www.applyword.http.CallBack;
import com.www.applyword.http.HttpClient;
import com.www.applyword.ui.home.HotJobDetailsActivity;
import com.www.applyword.utils.SPUtil;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;

/**
 * author : huang
 * date   : 2019/10/22 0022 15:16
 * version: 1.0
 * @author Administrator
 */
public class OrderSettlementFragment extends BaseFragment {
    @BindView(R.id.rl_job)
    RecyclerView rlJob;
    @BindView(R.id.refreshLayout)
    SmartRefreshLayout refresh;


    ViewStub vsEmpty;

    private HotJobAdapter mHotJobAdapter;
    private List<HotJobBean> hotJobBeans = new ArrayList<>();

    private int page = 1;
    private int Rows = 10;

    private List<View> popupViews = new ArrayList<>();
    private List<String> tabs = new ArrayList<>();
    private String headers[] = {"全武汉市", "职位类型", "离我最近", "综合筛选"};
    private String citys[] = {"不限", "武汉", "北京", "上海", "成都", "广州", "深圳", "重庆", "天津", "西安", "南京", "杭州"};
    private String city,dic,sex,position;

    @Override
    protected int getLayoutResource() {
        return R.layout.launched_fragment;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        initView();
        initDate(true);
        refresh.setOnRefreshListener(refreshLayout -> initDate(true));
        refresh.setOnLoadMoreListener(refreshLayout -> initDate(false));
        vsEmpty = rootView.findViewById(R.id.vs_empty);
        if (!EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().register(this);
        }
    }


    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(MessageEvent messageEvent) {
        if (messageEvent.getMessageKey() != null) {
            if (messageEvent.getMessageKey().equals(EventConstants.CITY)) {
                position = "";
                dic = "";
                sex = "";
                city = messageEvent.getMessageValue();
                initDate(true);
            }else if (messageEvent.getMessageKey().equals(EventConstants.DIC)){
                position = "";
                city = "";
                sex = "";
                dic = messageEvent.getMessageValue();
                initDate(true);
            }else if (messageEvent.getMessageKey().equals(EventConstants.SEX)){
                position = "";
                city = "";
                dic = "";
                sex = messageEvent.getMessageValue();
                initDate(true);
            }else {
                city = "";
                dic = "";
                sex = "";
                position = messageEvent.getMessageValue();
                initDate(true);
            }
        }
    }

    private void initView() {
        rlJob.setLayoutManager(new LinearLayoutManager(getActivity()));
        rlJob.setItemAnimator(new DefaultItemAnimator());
        mHotJobAdapter = new HotJobAdapter(getActivity());
        rlJob.setAdapter(mHotJobAdapter);
    }

    private void initDate(boolean isRefresh) { //最新发布
        String city2 = SPUtil.getString("city2");
        if (isRefresh) page = 1;
        Map<String, Object> params = new HashMap<>();
        params.put("Page", page + "");
        params.put("Rows", Rows + "");
        params.put("PositionType", 99 + "");
        if (!TextUtils.isEmpty(city)){
            params.put("AddressCity", city);
        }else {
            params.put("AddressCity", "");
        }
        if (!TextUtils.isEmpty(dic) && dic.equals("离我最近")){
            if (!TextUtils.isEmpty(city2) && !city2.contains("武汉")){
                params.put("AddressCity", city2);
            }else {
                params.put("AddressCity", SPUtil.getString("city"));
            }
        }else if (!TextUtils.isEmpty(dic) && dic.equals("最新发布")){
            if (!TextUtils.isEmpty(city2) && !city2.contains("武汉")){
                params.put("AddressCity", city2);
            }else {
                params.put("AddressCity", "");
            }
        }
        if (TextUtils.isEmpty(city) && TextUtils.isEmpty(dic) && !TextUtils.isEmpty(city2)){
            if (!city2.contains("武汉")){
                params.put("AddressCity", city2);
            }else {
                params.put("AddressCity", "");
            }
        }
      // params.put("AddressCity", "");
        if (!TextUtils.isEmpty(sex)){
            params.put("Sex", sex);
        }else {
            params.put("Sex", "");
        }
        params.put("FamousEnterprisesType", 99 + "");
        params.put("SalaryType", 0 + "");
        if (!TextUtils.isEmpty(position)){
            params.put("PositionID", position);
        }else {
            params.put("PositionID", "");
        }

        //职位福利
        params.put("Welfare", "");

        HttpClient.get(getActivity(), Constants.GETORDERLIST, params, new CallBack<List<OrderBean>>() {
            @Override
            public void onSuccess(List<OrderBean> orderBeans) {
                if (isRefresh) {
                    refresh.finishRefresh(true);
                    if (orderBeans.size() == 0) {
                        showEmpty(true, vsEmpty, 0);
                    } else {
                        showEmpty(false, vsEmpty, 0);
                    }
                    mHotJobAdapter.replaceAll(orderBeans, true);
                } else {
                    if (orderBeans.size() > 0) {
                        refresh.finishLoadMore(true);
                        mHotJobAdapter.addAll(orderBeans);
                    } else {
                        refresh.finishLoadMoreWithNoMoreData();
                    }
                }
                page++;
            }
        });

        mHotJobAdapter.setOnItemClickListener((viewHolder, view, i) -> startActivity(new Intent(getActivity(), HotJobDetailsActivity.class)
                .putExtra("OrderID", mHotJobAdapter.getItem(i).getOrder_ID())));
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        EventBus.getDefault().unregister(this);
    }
}

